import {
    request
} from "../../../request/index.js";
var redis = require("../../../utils/redis.js").default;
Page({
    data: {
        userInfo: {},
        verifyPasswordModal: true,
        bindModal: true,
        password: "",
        studentNo: ""
    },

    onShow: function () {
        this.getUserInfo();
    },
    async getUserInfo() {
        // let token = wx.getStorageSync('token');
        const token = redis.get('token');
        if (!token) {
            wx.navigateTo({
                url: '/pages/login/index'
            });
            return;
        }
        let userId = redis.get('userId');
        let res = await request({
            url: "/users/getUserInfo",
            method: 'get',
            data: {
                userId
            }
        })
        if (res.data.code === 200) {
            console.log("用户信息-获取用户信息：");
            console.log(res.data.data);
            let userInfo = res.data.data;
            if (userInfo.gender == 0) {
                userInfo.genderName = '男'
            } else if (userInfo.getder == 1) {
                userInfo.genderName = '女'
            } else {
                userInfo.genderName = '未知'
            }
            this.setData({
                userInfo
            })
        } else {
            console.log("用户信息-获取用户信息失败，" + res.data.msg);
        }
        console.log(res);
    },
    // 退出登录
    exit() {
        wx.showModal({
            title: '提示',
            content: '确认退出？',
            success(res) {
                if (res.confirm) {
                    wx.clearStorageSync();
                    wx.switchTab({
                        url: '/pages/ucenter/index',
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },
    verify() {
        this.setData({
            verifyPasswordModal: !this.data.verifyPasswordModal
        })
    },
    bind() {
        this.setData({
            bindModal: !this.data.bindModal
        })
    },
    bindinputPassword(event) {
        let password = event.detail.value;
        this.setData({
            password
        });
    },
    bindinputStudentNo(event) {
        let studentNo = event.detail.value;
        this.setData({
            studentNo
        });
    },
    async confirm() {
        let password = this.data.password;
        let studentNo = this.data.studentNo;
        if (!password) {
            wx.showToast({
                title: '请输入密码',
                icon: 'none'
            })
            return;
        }
        let userInfo = this.data.userInfo;
        userInfo.password = password;
        if (userInfo.studentNo) {
            // 解绑
            let res = await request({
                url: "/users/unbind",
                method: 'post',
                data: userInfo

            })
            if (res.data.code == 200) {
                wx.showToast({
                    title: '解绑成功',
                })
                this.verify();
                this.onLoad();
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'error'
                })
            }
        } else {
            // 绑定
            if (!studentNo) {
                wx.showToast({
                    title: '请输入学号',
                    icon: 'none'
                })
                return;
            }
            userInfo.studentNo = studentNo;
            let res = await request({
                url: "/users/bind",
                method: 'post',
                data: userInfo
            })
            if (res.data.code == 200) {
                wx.showToast({
                    title: '绑定成功',
                })
                this.bind();
                this.onLoad();
            } else {
                userInfo.studentNo = '';
                wx.showToast({
                    title: res.data.msg,
                    icon: 'error'
                })
            }
        }
    }
})